<template>
  <div>
    <a-card :bordered="false">
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button type="primary" @click="add">新增区域</a-button>
      </div>
      <!-- 表格展示 -->
      <div class="table-page-content">
        <custom-table
          ref="table"
          rowKey="id"
          :showAlert="false"
          :showSelection="false"
          :loading="loading"
          :columns="columns"
          :dataSource="pageData"
          @change="onTableChange"
        >
          <template slot="status" slot-scope="text">
            <span v-if="text == 1" style="color: rgba(112, 182, 3, 1)">使用中</span>
            <span v-if="text == 2" style="color: #e1354a;">停用</span>
          </template>

          <template slot="type" slot-scope="text">
            <span>{{ filterMultiDictText('area_type', text) }}</span>
          </template>
          <template slot="action" slot-scope="text, record">
            <a-button @click="handleDetails(record)" type="link" style="padding:0;">查看</a-button>
            <a-divider type="vertical" />
            <a-button type="link" @click="handleEdit(record)" style="padding:0;">编辑</a-button>
            <a-divider type="vertical" />
            <a-button
              type="link"
              v-if="record.status == 1"
              @click="handleStop(record)"
              style="padding:0;"
            >停用</a-button
            >
            <a-button
              type="link"
              v-if="record.status == 2"
              @click="handleStop(record)"
              style="padding:0;"
            >启用</a-button
            >
            <a-divider type="vertical" />
            <a-button type="link" @click="handleDelete(record)" style="padding:0;">删除</a-button>
          </template>
        </custom-table>
      </div>
    </a-card>
    <DrawModal
      :visible="isDrawModalShow"
      :isStatus="isStatus"
      :title="drawModalTitle"
      :regionType="pageFilter.regionType"
      :row="areasRow"
      @on-close="closePop"
      @on-submit="refreshTable"
    />
    <DetailModal ref="detailModalId" />
  </div>
</template>
<script>
import { getDictItemsFromCache } from '@/api/api'
import * as api from '@/api/xlgc/videoManage.js'
import CustomTable from '@/components/zxd/Table/CustomTable'
import DrawModal from './components/draw_modal'
import DetailModal from './components/detailModal'
export default {
  components: {
    CustomTable,
    DrawModal,
    DetailModal
  },
  data() {
    return {
      labelCol: { span: 5 },
      wrapperCol: { span: 18 },
      show: false,
      rangePickerTime: [],
      loading: false,
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` // 序号分页后连续
        },
        {
          title: '区域名称',
          dataIndex: 'areaName',
          width: 120,
          align: 'center'
        },
        {
          title: '监控设备',
          dataIndex: 'countCctv',
          width: 80,
          align: 'center'
        },
        {
          title: '授权部门数量',
          dataIndex: 'countGrantApart',
          width: 80,
          align: 'center'
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: 80,
          align: 'center',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'createTime' }
        },
        {
          title: '创建人',
          dataIndex: 'createBy',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'createBy' }
        },
        {
          title: '操作',
          width: 180,
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      pageFilter: {
        pageNo: 1,
        pageSize: 10
      },
      pageData: {},
      isDrawModalShow: false,
      drawModalTitle: '',
      areasRow: {},
      isStatus: 'add'
    }
  },
  created() {
    this.refreshTable()
  },
  mounted() {},
  methods: {
    onQuery() {
      this.refreshTable()
    },
    filterMultiDictText(dictCode, text) {
      if (!text) return
      let datas = getDictItemsFromCache(dictCode)
      // return this.$filterMultiDictText(datas, text)
      return this.$filterDictText(datas, text)
    },
    showForm() {
      this.show = !this.show
    },
    onResetForm() {
      this.pageFilter.pageNo = 1
      this.pageFilter.pageSize = 10
      this.rangePickerTime = []
      this.refreshTable()
    },
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = { currentPage: 'pageNo' }
      let paginations = Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations)
      this.refreshTable()
    },
    refreshTable() {
      this.loading = true
      api
        .areaList(this.pageFilter)
        .then(res => {
          if (res.success) {
            this.pageData = res.result
            this.loading = false
          } else {
            this.loading = false
          }
        })
        .catch(e => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    closePop(key) {
      this.isDrawModalShow = false
      this.areasRow = {}
    },
    // 新增
    add() {
      this.isDrawModalShow = true
      this.drawModalTitle = '新增区域'
      this.isStatus = 'add'
    },
    // 编辑
    handleEdit(record) {
      this.areasRow = record
      this.isStatus = 'edit'
      this.isDrawModalShow = true
      this.drawModalTitle = '编辑区域'
    },
    async handleStop(record) {
      this.loading = true
      let statusValue = record.status == 1 ? 2 : 1
      let res = await api.areaUpdateStatus({ id: record.id, status: statusValue })
      if (res.success) {
        this.refreshTable()
      } else {
        this.loading = false
      }
    },
    // 查看
    handleDetails(record) {
      this.$refs.detailModalId.show(record)
    },
    // 删除
    handleDelete(record) {
      var that = this
      this.$confirm({
        title: '确认删除',
        onOk: function() {
          that.loading = true
          api
            .areaDelete({ id: record.id })
            .then(res => {
              if (res.success) {
                that.$message.success('删除成功！')
                that.refreshTable()
              } else {
                that.loading = false
                that.$message.warning(res.message)
              }
            })
            .catch(() => {
              that.loading = false
              that.$message.warning(res.message)
            })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.mode_item {
  display: flex;
  border-bottom: 1px #dadada dashed;
  > div {
    flex: 0 0 25%;
  }
  & > * {
    width: 100%;
    margin-bottom: 0;
    padding: 5px 0;
  }
  &.mode_item_none {
    > div {
      flex: inherit;
    }
  }
  .zdy-form-item-label {
    /deep/.ant-form-item-label {
      width: 70px;
    }
  }
}
</style>
